<template>

  <div>
    <el-form :inline="true" style="margin-top: 5px">
      <el-form-item>
        <el-button type="primary" @click="dialogVisible = true">新增</el-button>
      </el-form-item>
      <el-form-item style="float: right">
        <el-input
            v-model="search"
            placeholder="输入用户名关键字搜索"
            align="right"
            height="80px">
          <!--        <template slot="prepend" icon="el-icon-search">搜索</template>-->
          <el-button slot="append" icon="el-icon-search" style="float: left"></el-button>
        </el-input>
      </el-form-item>
    </el-form>

    <el-table
        ref="multipleTable"
        :data="tableData.filter(data => !search || data.username.toLowerCase().includes(search.toLowerCase()))"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
        @selection-change="handleSelectionChange">
      <el-table-column
          prop="userId"
          label="用户id"
          width="120"
      >
      </el-table-column>
      <el-table-column
          prop="role"
          label="角色"
          width="120"
          :filters="[{ text: '管理员', value: 1 },{ text: '普通用户', value: 0 }]"
          :filter-method="filterTag"
          filter-placement="bottom-end">
        <template slot-scope="scope">
        <el-tag size="small" v-if="scope.row.role === 1" type="success">管理员</el-tag>
        <el-tag size="small" v-else-if="scope.row.role === 0" type="success">普通用户</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="username"
          label="用户名"
          >
      </el-table-column>
      <el-table-column
          prop="email"
          label="邮箱">
      </el-table-column>

      <el-table-column
          prop="ban"
          label="状态"
          :filters="[{ text: '禁用', value: 1 },{ text: '正常', value: 0 }]"
          :filter-method="filterTag1"
          filter-placement="bottom-end">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.ban === 0" type="success">正常</el-tag>
          <el-tag size="small" v-else-if="scope.row.ban === 1" type="danger">禁用</el-tag>
        </template>

      </el-table-column>

      <el-table-column
          prop="icon"
          label="操作">

        <template slot-scope="scope">

          <el-button size="mini"  type="danger" style="width:100px" v-if="scope.row.ban===0" @click="forbidden(scope.row.userId)">禁用</el-button>
          <el-button type="mini" style="width:100px" v-if="scope.row.ban===1" slot="reference" @click="notforbidden(scope.row.userId)">恢复</el-button>


        </template>
      </el-table-column>

    </el-table>

    <!--    分页-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[5,10,100,200,1000,10000]"
        :page-size="size"
        layout="total, sizes, prev, pager, next"
        :total="total"
        style="float: right">
    </el-pagination>

    <!--新增对话框-->
    <el-dialog
        title="新增用户"
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">

      <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-editForm">

        <el-form-item label="用户名" prop="username" label-width="100px"
        >
          <el-input v-model="editForm.username" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password" label-width="100px">
          <el-input v-model="editForm.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="邮箱" prop="email" label-width="100px">
          <el-input v-model="editForm.email" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="角色" prop="statu" label-width="100px">
          <el-radio-group v-model="editForm.role">
            <el-radio :label=0>普通用户</el-radio>
            <el-radio :label=1>管理员</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('editForm')">立即创建</el-button>
          <el-button @click="resetForm('editForm')">重置</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>


  </div>
</template>

<script>
export default {
  name: "userManage",
  data() {
    return {
      search:'',
      forbiddenForm: {
        userId:'',
        status:''
      },

      total: 0,
      size: 100,
      current: 1,

      dialogVisible:false,
      editForm: {
        username:'',
        password:'',
        email:'',
        role:''
      },
      editFormRules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],role: [
          {required: true, message: '请选择角色', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          { min: 6, message: '密码不能少于6位', trigger: 'blur' }
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'},
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        ban: [
          {required: true, message: '', trigger: 'blur'}
        ],

      },

      tableData: [
      ],

    }
  },
  created(){
    this.getUserList();

  },
  methods:{
    filterTag(value, row) {
      return row.role === value;
    },
    filterTag1(value, row) {
      return row.ban === value;
    },
    notforbidden(userId){
      this.forbiddenForm.userId=userId;
      this.forbiddenForm.status=0;
      this.$axios.post('/user/forbidden' , this.forbiddenForm)
          .then(res => {
            if(res.data.status === 200) {
              this.$message({
                showClose: true,
                message: '操作成功',
                type: 'success',
              });
              this.getUserList()
            }
          })
    },
    forbidden(userId){
      this.forbiddenForm.userId=userId;
      this.forbiddenForm.status=1;
      this.$axios.post('/user/forbidden' , this.forbiddenForm)
          .then(res => {
            if(res.data.status === 200) {
              this.$message({
                showClose: true,
                message: '操作成功',
                type: 'success',
              });
              this.getUserList()
            }
          })
    },
    getUserList() {
      this.$axios.get('/allUsers',{
        params:{
          size:this.size,
          current:this.current
        }
      }).then(res => {
        this.tableData = res.data.userList
        this.total= res.data.total
        this.current= res.data.current
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let psd = this.$md5(this.editForm.password);
          this.editForm.password=psd;
          this.$axios.post('/user/addManager' , this.editForm)
              .then(res => {

                if(res.data.status === 200) {
                  this.$message({
                    showClose: true,
                    message: '新增成功',
                    type: 'success',
                  });
                  this.getUserList()
                }
                this.dialogVisible = false
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
      this.editForm={}
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false
      this.editForm = {}
    },
    handleClose() {
      this.resetForm('editForm')
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size=val;
      this.getUserList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.current=val;
      this.getUserList();
    }
  }
}
</script>

<style scoped>

.el-pagination {
  float: right;
  margin-top: 22px;
}

</style>